/*

模拟登陆 csdn
https://blog.csdn.net/mengxiangxingdong
 */
/**
 * 第三方库
 */
const puppeteer = require('puppeteer'); //引入puppeteer库


/**
 * 本地库
 */
const {installMouseHelper} = require('./util/install-mouse-helper');


const xpathUtil = require('./util/XpathUtil');
(async () => {
    const browser = await puppeteer.launch({    //启动浏览器
        args: ['--no-sandbox'],
        headless: false,   //代码运行时打开浏览器方便观察
        // devtools:true   //打开f12界面
    });
    const page = await browser.newPage();  //打开浏览器的一个tab 页
    // Installs the helper to the page. Mouse will be visible in the subsequent navigation.
    await installMouseHelper(page); //调试鼠标轨迹专用
    //开启拦截选项
    // await page.setRequestInterception(true);
    //puppeteer 拦截设置cookie  存在bug  有时候cookie 并不能够完全收录
/*
    await page.on('request', request => {
        //直接请求
        request.continue();  //直接放行
        //获取本次请求的url
        //调用工具的请求进行请求
        //将返回的cookie  数据  进行对应的设置
    })
    //拦截当前page 返回的所有的响应
    await page.on('response', async response => {
        await  console.log(response.url());
        await  console.dir(response.headers());
        await  console.log('===========================================');
    })
*/

    await page.goto('https://passport.csdn.net/login', {timeout: 20 * 1000, waitUntil: 'networkidle0'});

    // await  console.log(await  page.content())
    await  page.waitForXPath('//a[contains(string(),"账号登录")]', {visible: true, timeout: 20 * 1000});
    await xpathUtil.$xClick(page, '//a[contains(string(),"账号登录")]');


    //等待账号密码显示
    await  page.waitForSelector('#all', {timeout: 10 * 1000});
    //输入账号密码
    await   page.type('#all', "", {delay: 100})
    await   page.type('#password-number', "", {delay: 100})
    //点击登录
    await xpathUtil.$xClick(page, '//button[contains(string(),"登录")]');
    await page.waitFor(1000) //比較卡
    //判断是否存在滑块验证码
    let isCodeVerify = await page.$('#nc_1_n1z').catch(e => null); //如果不存在返回null
    if (isCodeVerify) {
        //存在滑块验证码的处理逻辑   感觉也可以指直接修改style="left: 0px;"    变成宽度   nc-lang-cnt 减去 nc_1_n1z
        //滑块的按钮
        await  page.waitForSelector('#nc_1_n1z', {timeout: 10 * 1000})
        let startElement = await  page.$('#nc_1_n1z');
        let endElement = await  page.$('.nc-lang-cnt');
        //滑动的距离
        let startElementBox = await startElement.boundingBox();
        let endElementBox = await endElement.boundingBox();
        let startX = startElementBox.x + 10;
        let endX = startElementBox.x + (endElementBox.width);
        let startY = await  startElementBox.y + 10;
        let endY = await  endElementBox.y + 10;
        //按住鼠标
        // await  page.mouse.down(startX, startY);
        //移动鼠标
        // await  page.waitFor(10*1000)
        await  page.mouse.move(startX, startY);
        await  page.waitFor(3 * 1000)
        await  page.mouse.down(startX, startY);
        await  page.mouse.move(endX, endY);
        //松开鼠标
        await  page.mouse.up(endX, endY);
        console.dir(await startElement.boundingBox())
        console.dir(await endElement.boundingBox())
    }

    //等待网页跳转  networkidle0  不在有网络响应
    // const response = await  page.waitForNavigation({timeout: 10 * 1000, waitUntil: 'networkidle0'});
    // //输出响应的页面
    // console.log(await response.text())


    // await browser.close();  //关闭浏览器
})();